﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScript.Appl.Message;
using MixScriptWeb.control;
using MixScript.BLL.ApplAdmin;
using MixScriptWeb.Share;
using MixScript.Appl.Security.Crypto;
namespace MixScriptWeb.Member
{
    public partial class fUserMaintain : cCommonWeb// System.Web.UI.Page
    {
        private static DataTable dt;
        private cUserManagement oUM;
        private static SortDirection sd;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    addFindClickAttribute(this, new MixScriptTB[] { mixEmployeeID, mixEmployeeID }, new string[] { "SRCUSER", "SRCEMPID" });
                }
                mn = (MainMenu)this.Master;         

                
                hideStatusBar();
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }

        }

        protected void gvdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            
            gvdetail.PageIndex = e.NewPageIndex;
            //gvdetail.DataBind();
            bindGridview(gvdetail, dt);
            
        }

        protected void gvdetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            //DataTable dataTable = gvdetail.DataSource as DataTable;

            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }
            //if (dataTable != null)
            //{
                DataView dataView = new DataView(dt);
                dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

                bindGridview(gvdetail, dataView);
            //}
        }
      

        bool isvalid()
        {
            setValid(new MixScriptTB[] { mix1 });
            if (mix1.Text.Trim() == string.Empty)
            {
                mix1.isvalid = false;
                errMsg = "User ID " + em.getError(2);
                return false;
            }
            mix1.Text = mix1.Text.ToUpper();

            if (mixPassword.Text.Trim() == string.Empty)
            {
                mix1.isvalid = false;
                errMsg = "Password " + em.getError(2);
                return false;
            }
            if (mixPassword.Text != mixConfirm.Text)
            {
                mixConfirm.isvalid = false;
                mixPassword.isvalid = false;
                errMsg = em.getError(3);
                return false;
            }

            return true;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                if (isvalid())
                {
                    oUM = new cUserManagement();

                    //oUM.AddNewRowDB(out errMsg, "sy_user", new int[] { }, new string[] { }, mix1.Text, mixPassword.Text, mixEmployeeID.Text, '0', 0);



                    if (!oUM.isUSerExist(mix1.Text))
                    {

                        if (oUM.AddNewRowDB(out errMsg, "sy_user", new int[] { }, new string[] { }, true, mix1.Text, mixPassword.Text, mixEmployeeID.Text, '0', 0))
                        {
                            ShowStatusBar(errMsg);
                            btnFind_Click(btnFind, new EventArgs());
                            btnClear_Click(btnClear, new EventArgs());

                        }
                        else
                        {
                            ShowStatusBar(errMsg, msgType.msgError);
                        }
                    }
                    else
                    {
                        if (oUM.EditRowDtDB(out errMsg, "sy_user", new string[] { "userid" }, new object[] { mix1.Text }, true, mix1.Text, mixPassword.Text, mixEmployeeID.Text, '0', 0))
                        {
                            ShowStatusBar(errMsg);
                            btnFind_Click(btnFind, new EventArgs());
                            btnClear_Click(btnClear, new EventArgs());
                        }
                        else
                        {
                            ShowStatusBar(errMsg, msgType.msgError);
                        }

                    }

                }
                else
                {
                    ShowStatusBar(errMsg, msgType.msgError);
                }

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }
        }



        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {

                setValid(new MixScriptTB[] { mix1, mixConfirm, mixEmployeeID, mixPassword });
                //                DataGridViewSelectedRowCollection dgvrow = gv.SelectedRows;
                if (mix1.Text.Trim() != "ADMINISTRATOR")
                {

                    if (mix1.Text != string.Empty)
                    {

                        oUM = new cUserManagement();
                        oUM.deleteRowDB(new string[] { "userid" }, new object[] { mix1.Text }, "sy_user", out errMsg);
                        //mf.setStatusMessage(msg, MessageType.OK);             

                        btnClear_Click(btnClear, new EventArgs());
                        btnFind_Click(btnFind, new EventArgs());
                        ShowStatusBar(errMsg);
                    }
                    else
                    {
                        ShowStatusBar(em.getError(5));
                    }


                }
                else
                {
                    //mf.setStatusMessage("USERID ini tidak diperkenankan USERID: ADMINISTRATOR", MessageType.Error);
                    ShowStatusBar(em.getError(4));
                }


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }





        protected void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                oUM = new cUserManagement();
                dt = new DataTable();
                if (oUM.getUser(ref dt, mix1.Text))
                {

                }
                else
                {
                    ShowStatusBar("Record Not Found", msgType.msgError);
                }
                gvdetail.DataSource = dt;
                gvdetail.DataBind();



            }
            catch (Exception ex)
            {

                throw;
            }

        }

        protected void btnREset_Click(object sender, EventArgs e)
        {
            //  mix1.Text = "sfdssd";
            mix1.showFind = false;
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {


                //            Response.Redirect(Page.AppRelativeVirtualPath);
                clearMixTB(new MixScriptTB[] { mix1, mixConfirm, mixPassword, mixEmployeeID });



            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }
        }

        protected void gvdetail_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                oUM = new cUserManagement();

                if (gvdetail.SelectedIndex > -1)
                {
                    mix1.Text = gvdetail.SelectedRow.Cells[1].Text;
                    mixEmployeeID.Text = gvdetail.SelectedRow.Cells[2].Text.Trim();
                    mixPassword.Text = oUM.getUserPass(gvdetail.SelectedRow.Cells[1].Text);
                    
                    removeNbsp(new MixScriptTB[] { mixEmployeeID, mixPassword, mixConfirm });
                                        
                    

                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

    

    }
}